<?php

namespace App\Http\Controllers\Api;

use App\Cinema;
use App\OnlineFilmticket;
use App\PricePlan;
use App\Priceplantickettypes;
use App\Screen;
use App\TicketType;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Mockery\Exception;

class CinemasController extends BaseController
{

    public $user;

    public function __construct(Request $request)
    {
        parent::__construct();
        $this->user = $this->is_login($request);
    }



    //查看影院信息
    public function index(Request $request){

        if (!$this->user['data']->can('add_cinema')){
            return error('NoPermission');
        }

        $id = $this->user['data']['cinema_id'];
        if($data = Cinema::where('status','!=',3)->find($id)){
            return success($data,'查询成功');
        }else{
            return error('查询失败');
        }
    }

    /**
     * @param Request $request
     * 完善影院信息
     */
    public function complete_cinema_info(Request $request){

        if (!$this->user['data']->can('add_cinema')){
            return error('NoPermission');
        }

        $user_id = $this->user['data']['id'];
        $user_type = $this->user['data']['type'];
        if($user_type !=2){
            return error('非影院用户不可完善影院信息');
        }

        $cinema_info = Cinema::where('status','!=',3)->find($user_id);

        if (!$cinema_info){
            return error('影院不存在或已删除');
        }

        if ($cinema_info->code){
            return error('影院已绑定影院编码');
        }

        if (!$business_date=$request->business_date){
            return error('影院营业日信息缺失');
        }

        $cinema_info->business_date = $business_date;


        if ($cinema_info->save()){
            logs($this->user,'完善影院'.$cinema_info->name.'信息成功');
            return success($cinema_info,'影院信息已完善');
        }else{
            logs($this->user,'完善影院'.$cinema_info->name.'信息失败');
            return error('完善信息失败');
        }
    }

    /**
     * 设置影院营业状态
     */
    public function set_operating_status(Request $request){

        if (!$this->user['data']->can('add_cinema')){
            return error('NoPermission');
        }

        if (!$id = $request->id){
            return error('影院id缺失');
        }
        if (!$status = $request->status){
            return error('影院状态缺失');
        }

        if (!in_array($status,[1,2])){
            return error('影院状态错误');
        }

        $cinema_info = Cinema::find($id);
        if (!$cinema_info){
            return error('影院信息不存在');
        }
        $cinema_info->status = $status;

        $str = http_build_query([
            'request'=>$status
        ]);

        $res = curl($_ENV['SERVER_D'].'/CinemaStateChange',$str,false);
        

        $res = json_decode($res,true);

        // $cinema_status = [1=>2,2=>1];
        if ($res&&$res['return_value'] ==0){
            $cinema_info->name = $res['name'];
            $cinema_info->corporation = $res['corporation'];
            $cinema_info->contact = $res['contact'];
            $cinema_info->telephone = $res['telephone'];
            $cinema_info->fax = $res['fax'];
            $cinema_info->chain_name = $res['cinemas'];
//            $cinema_info->status = $cinema_status[$res['status']];
            $cinema_info->status = $res['status'];
            $cinema_info->screen_count = $res['screen_count'];
            $cinema_info->save();
            return success($cinema_info,'设置状态成功');
        }else{
            return error('设置影院状态失败');
        }


    }

    /*
     * 编辑影院信息
     */
    public function edit(Request $request){

        if (!$this->user['data']->can('add_cinema')){
            return error('NoPermission');
        }

        if (!$business_date = trim($request->business_date)){
            return error('营业日缺失');
        }


        $id = $this->user['data']['cinema_id'];
        $cinema = Cinema::where('status','!=',3)->find($id);

        if (!$cinema){
            return error('影院已被删除');
        }

        $cinema->business_date = $business_date;
        $cinema->corporation = $request->corporation;
        $cinema->contact = $request->contact;
        $cinema->telephone = $request->telephone;
        $cinema->chain_code = '10000000';
        $cinema->address = $request->address;
        $cinema->screen_count = $request->screen_count==''?0:$request->screen_count;
        $cinema->province = $request->province;
        $cinema->city = $request->city;
        $cinema->district = $request->district;

        if ($cinema->save()){
            logs($this->user,'编辑影院'.$cinema->name.'信息成功');
            return success($cinema->id,'编辑成功');
        }else{
            logs($this->user,'编辑影院'.$cinema->name.'信息失败');
            return error('编辑失败');
        }
    }


    /**
     * @param Request $request
     * 查看票价类型列表
     */
    public function get_ticket_type_list(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        $ticket_type_list = TicketType::where('status','!=',2)->where('cinema_code','=',$this->user['data']['cinema_code'])->get();
        return success($ticket_type_list,'查询成功');
    }

    /**
     * @param Request $request
     * 添加票类
     */
    public function add_ticket_type(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$name=trim($request->input('name'))){
            return error('票价名称必填！');
        }
        if (TicketType::where('status','!=',2)->where(['name'=>$name,'cinema_code'=>$cinema_code])->count()>0){
            return error('票价类型已存在');
        }

        if(!$ticket_price=trim($request->input('ticket_price'))){
            return error('票价必填!');
        }
        $ticket_type = new TicketType();
        $ticket_type->cinema_code = $cinema_code;
        $ticket_type->name = $name;
        $ticket_type->ticket_price = $ticket_price;
        $ticket_type->status = 1;
        if($ticket_type->save()){
            logs($this->user,'添加票类'.$name.'成功');
            return success($ticket_type->id,'添加成功');
        }else{
            logs($this->user,'添加票类'.$name.'失败');
            return error('添加失败');
        }
    }

    /**
     * @param Request $request
     * 编辑票类
     */
    public function edit_ticket_type(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        if(!$ticket_price=trim($request->input('ticket_price'))){
            return error('票价必填!');
        }

        if (!$id = trim($request->input('id'))){
            return error('票类ID缺失');
        }

        if (!$name = trim($request->input('name'))){
            return error('票类名称缺失');
        }

        $ticket_type = TicketType::where('status','!=','2')->find($id);
        if (!$ticket_type){
            return error('票类不存在');
        }

        $ticket_type->ticket_price = $ticket_price;
        $ticket_type->name = $name;
        if ($ticket_type->save()){
            logs($this->user,'编辑票类'.$name.'成功');
            return success($ticket_type->id,'编辑成功');
        }else{
            logs($this->user,'编辑票类'.$name.'失败');
            return error('编辑失败');
        }
    }

    /**
     * @param Request $request
     * 删除票类
     */
    public function delete_ticket_type(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        if (!trim($id = $request->input('id'))){
            return error('票类ID缺失');
        }

        $ticket_type = TicketType::where('status','!=','2')->find($id);
        if (!$ticket_type){
            return error('票类不存在');
        }
        $count = \DB::table('priceplan_tickettypes')->where(['ticket_type_id'=>$id])->count();
        if($count>0){
            return error('票类存在关联票价类型');
        }

        $ticket_type->status = 2;
        if ($ticket_type->save()){
            logs($this->user,'删除票类'.$ticket_type->name.'成功');
            return success($ticket_type->id,'删除成功');
        }else{
            logs($this->user,'删除票类'.$ticket_type->name.'失败');
            return error('删除失败');
        }
    }

    /**
     * @param Request $request
     * 票价方案列表
     */
    public function pricing_plan_list(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        $data = PricePlan::where(['status'=>1,'cinema_code'=>$cinema_code])->get();
        foreach ($data as $key=>$val){
            $price_plan_ticket_types = \DB::table('priceplan_tickettypes')->where('price_plan_id','=',$val['id'])->pluck('ticket_type_id');
            $ticket_type_ids = [];
            foreach ($price_plan_ticket_types as $v){
                $ticket_type_ids[] = $v;
            }
            $data[$key]['ticket_type_ids'] = json_encode($ticket_type_ids);
        }
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 添加票价方案
     */
    public function add_pricing_plan(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];

        if (!$name = trim($request->input('name'))){
            return error('票价方案名称必填');
        }
        if (PricePlan::where(['status'=>1,'cinema_code'=>$cinema_code,'name'=>$name])->count()>0){
            return error('票价方案已存在');
        }
        if (!$default_price = trim($request->input('default_price'))){
            return error('网售标准票价必填');
        }
        if (!$local_default_price = trim($request->input('local_default_price'))){
            return error('本地标准票价必填');
        }
        $price_plan = new PricePlan();
        $price_plan->cinema_code = $cinema_code;
        $price_plan->name = $name;
        $price_plan->default_price = $default_price;
        $price_plan->local_default_price = $local_default_price;
        $price_plan->remark = $request->input('remark');
        $price_plan->status = 1;
        if($price_plan->save()){
            logs($this->user,'添加票价方案'.$name.'成功');
            return success($price_plan->id,'添加成功');
        }else{
            logs($this->user,'添加票价方案'.$name.'失败');
            return error('添加失败');
        }

    }

    /**
     * 编辑票价方案
     */
    public function edit_pricing_plan(Request $request)
    {

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];

        if (!$name = trim($request->input('name'))) {
            return error('票价方案名称必填');
        }
        if (!$id = trim($request->input('id'))) {
            return error('票价方案ID缺失');
        }
        if (!$default_price = trim($request->input('default_price'))) {
            return error('网售标准票价必填');
        }
        if (!$local_default_price = trim($request->input('local_default_price'))){
            return error('本地标准票价必填');
        }
        $price_plan = PricePlan::where(['status' => 1, 'cinema_code' => $cinema_code])->find($id);
        if (!$price_plan) {
            return error('票价方案不存在');
        }
        $ticket_type_ids = trim($request->input('ticket_type_ids'));
        $ticket_type_ids = explode(',', $ticket_type_ids);
        \DB::table('priceplan_tickettypes')->where(['price_plan_id' => $id])->delete();
        foreach ($ticket_type_ids as $val) {
            $data['price_plan_id'] = $id;
            $data['ticket_type_id'] = $val;
            $arr[] = $data;
        }
        \DB::beginTransaction();
        try {
            $res = \DB::table('priceplan_tickettypes')->insert($arr);
            $price_plan->remark = $request->input('remark');
            $price_plan->default_price = $default_price;
            $price_plan->local_default_price = $local_default_price;
            $price_plan->name = $name;
            $res_price_plan = $price_plan->save();
            if ($res&&$res_price_plan){
                \DB::commit();
                logs($this->user,'编辑票价方案'.$name.'成功');
                return success('','编辑成功');
            }
        }catch (\Exception $e){
            \DB::rollBack();
            logs($this->user,'编辑票价方案'.$name.'失败');
            return error('编辑失败');
        }


    }

    /**
     * 删除票价方案
     */
    public function delete_pricing_plan(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!trim($id = $request->input('id'))){
            return error('票价方案ID缺失');
        }
        $price_plan = PricePlan::where(['status'=>1,'cinema_code'=>$cinema_code])->find($id);
        if (!$price_plan){
            return error('票价方案不存在');
        }
        $price_plan->status = 2;
        \DB::table('priceplan_tickettypes')->where(['price_plan_id'=>$id])->delete();
        if ($price_plan->save()){
            logs($this->user,'删除票价方案'.$price_plan->name.'成功');
            return success($price_plan->id,'删除成功');
        }else{
            logs($this->user,'删除票价方案'.$price_plan->name.'失败');
            return error('删除失败');
        }
    }

    /**
     * @param Request $request
     * 票价方案关联票价类型
     */
    public function plan_add_type(Request $request){

        if (!$this->user['data']->can('ticket_manager')){
            return error('NoPermission');
        }

        if (!trim($id=$request->input('id'))){
            return error('票价方案ID缺失');
        }

        $priceplan = PricePlan::where(['status'=>1])->find($id);
        if (!$priceplan){
            return error('票价方案不存在');
        }

        if (!$ticket_type_ids= trim($request->input('ticket_type_ids'))){
            return error('票价类型id不能为空');
        }
        $ticket_type_ids = json_decode($ticket_type_ids,true);
        \DB::table('priceplan_tickettypes')->where(['price_plan_id'=>$id])->delete();
        foreach ($ticket_type_ids as $val){
            $data['price_plan_id'] = $id;
            $data['ticket_type_id'] = $val;
            $arr[] = $data;
        }
        $res = \DB::table('priceplan_tickettypes')->insert($arr);
        if($res){
            return success('','关联成功');
        }else{
            return error('关联失败');
        }
    }


    /**
     * @param Request $request
     * 添加网售渠道商
     */
    public function add_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('cinema_online_ticketer')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$online_user_id = trim($request->online_user_id)){
            return error('渠道商必填');
        }
        $online_user_info = User::where(['status'=>1,'type'=>3])->find($online_user_id);
        if (!$online_user_info){
            return error('渠道商不存在或已终止合作');
        }
        $count = OnlineFilmticket::where(['cinema_code'=>$cinema_code,'status'=>1,'online_user_id'=>$online_user_id])->first();
        if ($count){
            return error('渠道商已存在');
        }
        if (!$service=$request->service){
            return error('服务费必填');
        }
        $online_film_ticketer = new OnlineFilmticket();
        $online_film_ticketer->online_user_id = $online_user_id;
        $online_film_ticketer->service = $service;
        $online_film_ticketer->cinema_code = $cinema_code;
        if ($online_film_ticketer->save()){
            logs($this->user,'添加网售渠道商'.$online_user_info->name.'成功');
            return success($online_film_ticketer->id,'添加成功');
        }else{
            logs($this->user,'添加网售渠道商'.$online_user_info->name.'失败');
            return error('添加失败');
        }

    }

    /**
     * @param Request $request
     * 编辑网售渠道商
     */
    public function set_online_film_ticket(Request $request){

        if (!$this->user['data']->can('cinema_online_ticketer')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$online_user_id = $request->online_user_id){
            return error('影院关联网售ID缺失');
        }
        $online_film_ticket = OnlineFilmticket::where(['cinema_code'=>$cinema_code,'online_user_id'=>$online_user_id])->where('status','!=',3)->first();
        if (!$online_film_ticket){
            return error('影院关联网售渠道商信息不存在或已删除');
        }
        if (!$service = $request->service){
            return error('服务费必填');
        }
        $online_film_ticket->service = $service;
        if ($online_film_ticket->save()){
            logs($this->user,'编辑网售渠道商'.$online_film_ticket->name.'成功');
            return success($online_film_ticket->id,'编辑成功');
        }else{
            logs($this->user,'编辑网售渠道商'.$online_film_ticket->name.'失败');
            return error('编辑失败');
        }

    }

    /**
     * @param Request $request
     * @return array
     * 影院暂停与网售渠道商
     */
    public function  stop_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('cinema_online_ticketer')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$online_user_id = $request->online_user_id){
            return error('影院关联网售ID缺失');
        }
        $online_film_ticket = OnlineFilmticket::where(['cinema_code'=>$cinema_code,'online_user_id'=>$online_user_id])->where('status','=',1)->first();
        if (!$online_film_ticket){
            return error('影院关联网售渠道商信息不存在或已删除、已暂停');
        }
        $online_film_ticket->status = 3;
        if ($online_film_ticket->save()){
            logs($this->user,'暂停网售渠道商'.$online_film_ticket->name.'成功');
            return success($online_film_ticket->id,'暂停成功');
        }else{
            logs($this->user,'暂停网售渠道商'.$online_film_ticket->name.'失败');
            return error('暂停失败');
        }
    }

    /**
     * @param Request $request
     * @return array
     * 影院恢复与网售渠道商
     */
    public function restore_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('cinema_online_ticketer')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$online_user_id = $request->online_user_id){
            return error('影院关联网售ID缺失');
        }
        $online_film_ticket = OnlineFilmticket::where(['cinema_code'=>$cinema_code,'online_user_id'=>$online_user_id])->first();
        if (!$online_film_ticket){
            return error('影院关联网售渠道商信息不存在');
        }
        $online_film_ticket->status = 1;
        if ($online_film_ticket->save()){
            logs($this->user,'恢复网售渠道商'.$online_film_ticket->name.'成功');
            return success($online_film_ticket->id,'已恢复');
        }else{
            logs($this->user,'恢复网售渠道商'.$online_film_ticket->name.'失败');
            return error('恢复失败');
        }
    }

    /**
     * @param Request $request
     * @return array
     * 影院删除与网售渠道商
     */
    public function delete_online_film_ticketer(Request $request){

        if (!$this->user['data']->can('cinema_online_ticketer')){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        if (!$online_user_id = $request->online_user_id){
            return error('影院关联网售ID缺失');
        }
        $online_film_ticket = OnlineFilmticket::where(['cinema_code'=>$cinema_code,'online_user_id'=>$online_user_id])->where('status','!=',2)->first();
        if (!$online_film_ticket){
            return error('影院关联网售渠道商信息不存在');
        }
        $online_film_ticket->status = 2;
        if ($online_film_ticket->save()){
            logs($this->user,'删除网售渠道商'.$online_film_ticket->name.'成功');
            return success($online_film_ticket->id,'删除成功');
        }else{
            logs($this->user,'删除网售渠道商'.$online_film_ticket->name.'失败');
            return error('删除失败');
        }
    }

    /**
     * @param Request $request
     * 查看网售渠道商列表
     */
    public function get_online_film_ticketer(Request $request){

        if (!$this->user['data']->can(['cinema_online_ticketer','admin_refund_ticket'])){
            return error('NoPermission');
        }

        $cinema_code = $this->user['data']['cinema_code'];
        $list = \DB::table('online_filmtickets');
        $data = $list->where(['online_filmtickets.cinema_code'=>$cinema_code])
            ->join('users','users.id','=','online_filmtickets.online_user_id')
            ->select('online_filmtickets.*','users.name','users.account')
            ->orderBy('online_filmtickets.status')
            ->paginate('15');
        return success($data,'查询成功');
    }

    /**
     * @param Request $request
     * 查看影院未合作渠道商列表
     */
    public function get_not_cooperation_film_ticketer(Request $request){
        if (!$this->user['data']->can('cinema_online_ticketer')){
            return error('NoPermission');
        }
        $cinema_code = $this->user['data']['cinema_code'];
        $cooperation_film_ticketer = OnlineFilmticket::where(['cinema_code'=>$cinema_code])->pluck('online_user_id');
        $not_cooperation_film_ticket_list = User::where(['status'=>1,'type'=>3])->whereNotIn('id',$cooperation_film_ticketer)->get();
        return success($not_cooperation_film_ticket_list,'查询成功');
    }




}
